#include "LocalScene.h"
#include "Utils/LogUtil.h"
#include "LocalEntity.h"
#include "Utils/StringUtil.h"
#include "Game/Component/Model.h"
#include "Render/RenderState.h"
#include "Render/Imgui/imgui.h"
#include "Math/Random.h"


void LocalScene::OnInit()
{
	mCoordinateProcess = GetProcess<CoordinateProcess>("CoordinateProcess");
	mPbrProcess = GetProcess<PbrProcess>("PbrProcess");

	GeometryGenerator geometryGenerator;
	GeometryGenerator::MeshData sphereData = geometryGenerator.CreateGeosphere(0.2f, 5);


	mLightActives.resize(4, 1);
	mLightEntitys.resize(4, nullptr);
	for (uint32 i = 0; i < 4; ++i)
	{
		LocalEntity* lightEntity = CreateEntity<LocalEntity>();
		lightEntity->AddComponent<Model>(sphereData);
		glm::vec3 color(1.f);
		glm::vec3 pos(6.f, 6.f, 6.f);
		switch (i)
		{
		case 1:
			color = glm::vec3(1.f, 0.f, 0.f);
			pos = glm::vec3(6.f, -6.f, 6.f);
			break;
		case 2:
			color = glm::vec3(0.f, 1.f, 0.f);
			pos = glm::vec3(-6.f, 6.f, 6.f);
			break;
		case 3:
			color = glm::vec3(0.f, 0.f, 1.f);
			pos = glm::vec3(-6.f, -6.f, 6.f);
			break;
		}
		lightEntity->AddComponent<Light>(color);
		lightEntity->GetTransform().SetLocalPosition(pos);
		lightEntity->GetTransform().UpdateMatrices();
		mLightEntitys[i] = lightEntity;
	}


	GeometryGenerator::MeshData bigSphereData = geometryGenerator.CreateGeosphere(0.6f, 40);
	// GeometryGenerator::MeshData bigSphereData = geometryGenerator.CreateSphere(0.6f, 20, 20);

	LocalEntity* sphere = CreateEntity<LocalEntity>("Sphere");
	sphere->AddComponent<Model>(bigSphereData);
	sphere->AddComponent<PbrType>();

	LocalEntity* torusknot = CreateEntity<LocalEntity>("Torusknot");
	torusknot->AddComponent<Model>("Torusknot.obj");
	torusknot->AddComponent<PbrType>();
	torusknot->GetTransform().SetLocalScale(glm::vec3(0.8f));
	torusknot->GetTransform().UpdateMatrices();

	LocalEntity* venus = CreateEntity<LocalEntity>("Venus");
	venus->AddComponent<Model>("Venus.obj");
	venus->AddComponent<PbrType>();
	venus->GetTransform().SetLocalScale(glm::vec3(0.2f));
	venus->GetTransform().UpdateMatrices();

	LocalEntity* teapot = CreateEntity<LocalEntity>("Teapot");
	teapot->AddComponent<Model>("Teapot.obj");
	teapot->AddComponent<PbrType>();
	teapot->GetTransform().SetLocalScale(glm::vec3(0.2f));
	teapot->GetTransform().UpdateMatrices();
}

void LocalScene::OnExit()
{

}

void LocalScene::OnTick(float deltaTime)
{

}

void LocalScene::OnImgui()
{
	GameScene::OnImgui();

#if 1

	ImGui::SetNextWindowPos(ImVec2(20, 20), ImGuiCond_FirstUseEver);

	ImGui::Begin("PbrOption");

	for (uint32 i = 0; i < 4; ++i)
	{
		bool* isActive = ((bool*)mLightActives.data()) + i;
		const std::string lightName = STRFORMAT("Light %d", i);
		if (ImGui::Checkbox(lightName.c_str(), isActive))
		{
			mLightEntitys[i]->GetComponent<Light>().mActive = (bool)*isActive;
			// LOGI(L"Light Active %d --> %d", i, (uint8)*isActive);
		}
	}

	ImGui::Combo("ModelSelect", &mPbrProcess->mObjectIndex, &mPbrProcess->mObjectNames[0], 4, 4);

	ImGui::Combo("MaterialSelect", &mPbrProcess->mMaterialIndex, &mPbrProcess->mMaterialNames[0], 11, 11);

	ImGui::RadioButton("Pbr", &mPbrProcess->mWatchMode, 0);
	ImGui::RadioButton("Roughness", &mPbrProcess->mWatchMode, 1);
	ImGui::RadioButton("Metallic", &mPbrProcess->mWatchMode, 2);
	ImGui::RadioButton("Fresnel 0", &mPbrProcess->mWatchMode, 3);
	ImGui::RadioButton("Fresnel", &mPbrProcess->mWatchMode, 4);
	ImGui::RadioButton("Albedo", &mPbrProcess->mWatchMode, 5);
	ImGui::RadioButton("No Gamma", &mPbrProcess->mWatchMode, 6);
	ImGui::RadioButton("Lo", &mPbrProcess->mWatchMode, 7);
	ImGui::RadioButton("Lo and Gamma", &mPbrProcess->mWatchMode, 8);
	ImGui::RadioButton("Fresnel not a*a", &mPbrProcess->mWatchMode, 9);

	//if (mLightEntitys.size() > 0)
	//{
	//	Light& light0 = mLightEntitys[0]->GetComponent<Light>();
	//	ImGui::Checkbox("Light0", &light0.mActive);
	//	Light& light1 = mLightEntitys[1]->GetComponent<Light>();
	//	ImGui::Checkbox("Light1", &light1.mActive);
	//	Light& light2 = mLightEntitys[2]->GetComponent<Light>();
	//	ImGui::Checkbox("Light2", &light2.mActive);
	//	Light& light3 = mLightEntitys[3]->GetComponent<Light>();
	//	ImGui::Checkbox("Light3", &light3.mActive);
	//}

	/*ImGui::DragFloat("U_Scale", &mNormalProcess->mUScale, 0.005f);
	ImGui::DragFloat("V_Scale", &mNormalProcess->mVScale, 0.005f);

	ImGui::InputFloat("HeightScale", &mNormalProcess->mHeightScale, 0.025f, 0.25f);
	ImGui::InputFloat("ParallaxBias", &mNormalProcess->mParallaxBias, 0.025f, 0.25f);
	ImGui::InputFloat("NumLayers", &mNormalProcess->mNumLayers, 0.025f, 0.25f);

	ImGui::RadioButton("Color", &mNormalProcess->mMappingMode, 0);
	ImGui::RadioButton("Normal", &mNormalProcess->mMappingMode, 1);
	ImGui::RadioButton("Parallax", &mNormalProcess->mMappingMode, 2);
	ImGui::RadioButton("SteepParallax", &mNormalProcess->mMappingMode, 3);
	ImGui::RadioButton("ParallaxOcclusion", &mNormalProcess->mMappingMode, 4);*/

	ImGui::End();

#endif
}
